﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class AllSparePartApplyForms : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSparePartApplyForm();
            }
        }

        private void BindSparePartApplyForm()
        {
            WXYW.DAL.User currentUser = ContextService.CurrentAccount;
            List<SparePartApplyForm> spaForms = new List<SparePartApplyForm>();
            SparePartApplyService service = new SparePartApplyService();
            if (currentUser.UserType.ID == (int)RoleType.Administrator || currentUser.UserType.ID == (int)RoleType.SparePart)
            {
                spaForms = service.GetAll(keyWordTextBox.Text);
            }
            else
            {
                spaForms = service.GetSparePartApplyFormByApplicant(currentUser.UserID,keyWordTextBox.Text).ToList();
            }
            RecordsNumber = spaForms.Count;
            sparePartApplyFormsGridView.DataSource = spaForms;
            sparePartApplyFormsGridView.DataBind();
        }

        //private string RedirectTo(string status, string sparePartApplyFormID)
        //{
        //    string URL = string.Empty;
        //    switch (status)
        //    {
        //        case "备件-待审批":
        //            URL = "SparePartApplyConfirmApplication.aspx";
        //            break;
        //        case "备件-不同意":
        //            URL = "UpgradeDisagree.aspx";
        //            break;
        //        case "备件-同意待发货":
        //            URL = "SparePartApplySendProduct.aspx";
        //            break;
        //        case "升级-发货待收货确认":
        //            URL = "SparePartApplyConfirmReceiveProduct.aspx";
        //            break;
        //        case "备件-待返还":
        //            URL = "";
        //            break;
        //    }
        //    return string.Format(URL + "?{0}={1}", Constants.QUERYSTR_SPAREPARTAPPLY_ID, sparePartApplyFormID);
        //}

        protected void updateLinkButton_Click(object sender, EventArgs e)
        {
            string sparePartApplyID = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("SparePartApplyDisagree.aspx?{0}={1}", Constants.QUERYSTR_SPAREPARTAPPLY_ID, sparePartApplyID));
        }

        protected void sparePartApplyFormsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < sparePartApplyFormsGridView.PageCount)
            {
                sparePartApplyFormsGridView.PageIndex = e.NewPageIndex;
                BindSparePartApplyForm();
            }
        }

        protected void sparePartApplyFormsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                //SparePartApplyForm form = (SparePartApplyForm)e.Row.DataItem;
                //if (form.Status != "备件-不同意")
                //{
                //    LinkButton upgradeButton = e.Row.FindControl("updateLinkButton") as LinkButton;
                //    upgradeButton.Enabled = false;
                //}
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= sparePartApplyFormsGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (sparePartApplyFormsGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<SparePartApplyForm> dds = new List<SparePartApplyForm>();
            dds.Add(new SparePartApplyForm());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            sparePartApplyFormsGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindSparePartApplyForm();
        }

        protected void sparePartApplyFormsGridView_DataBound(object sender, EventArgs e)
        {
            if (sparePartApplyFormsGridView.BottomPagerRow != null)
            {
                sparePartApplyFormsGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void sparePartApplyFormsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            WXYW.DAL.User currentUser = ContextService.CurrentAccount;
            IQueryable<SparePartApplyForm> spaForms;
            SparePartApplyService service = new SparePartApplyService();
            if (currentUser.UserType.ID == (int)RoleType.Administrator || currentUser.UserType.ID == (int)RoleType.SparePart)
            {
                spaForms = service.GetAll(keyWordTextBox.Text).AsQueryable();
            }
            else
            {
                spaForms = service.GetSparePartApplyFormByApplicant(currentUser.UserID,keyWordTextBox.Text);
            }
            RecordsNumber = spaForms.Count();

            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }

            sparePartApplyFormsGridView.DataSource = spaForms.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            sparePartApplyFormsGridView.DataBind();
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindSparePartApplyForm();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindSparePartApplyForm();
        }

        protected void rmalnk_Click(object sender, EventArgs e)
        {
            LinkButton lnkBtn = sender as LinkButton;
            string back_url = Request.Url.ToString();
            string url = string.Format("RMALinkDetail.aspx?APPLICATIOM_ID={0}&" + Constants.QUERYSTR_BACK_URL + "={1}",
                lnkBtn.CommandArgument, back_url);
            Response.Redirect(url);
        }
    }
}
